package com.lizhaoshan.controller;

import cn.hutool.core.collection.CollUtil;
import com.lizhaoshan.common.R;
import com.lizhaoshan.common.Result;
import com.lizhaoshan.dto.CategoryDto;
import com.lizhaoshan.pojo.Category;
import com.lizhaoshan.pojo.PageBean;
import com.lizhaoshan.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @program: reggie
 * @description: 分类控制层
 * @author: lamour
 * @create: 2022-05-14 19:27
 **/
@Slf4j
@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public Result addCategory(@RequestBody CategoryDto categoryDto){
        log.info("**********分类添加，begin");

        if (Objects.isNull(categoryDto)){
            return Result.error("暂无数据");
        }

        boolean result = categoryService.addCategory(categoryDto);

        log.info("**********分类添加,end");
        return result?Result.success("添加成功"):Result.error("添加失败");

    }

    @PutMapping
    public Result putCategory(@RequestBody CategoryDto categoryDto){
        log.info("**********分类修改，begin");

        if (Objects.isNull(categoryDto)){
            return Result.error("暂无数据");
        }

        boolean result = categoryService.putCategory(categoryDto);

        log.info("**********分类修改，end");
        return result?Result.success("修改分类信息成功"):Result.error("修改分类信息失败");
    }

    @DeleteMapping
    public Result deleteCategory(@RequestParam String id){
        log.info("**********分类删除，begin");

        if (Objects.isNull(id)){
            return Result.error("暂无数据");
        }

        boolean result = categoryService.deleteCategory(id);

        log.info("**********分类删除，end");
        return result?Result.success("分类删除成功"):Result.error("分类删除失败");

    }

    @GetMapping("/list")
    public Result getCategoryType(@RequestParam Integer  type){
        log.info("**********通过type分类下拉查询，begin");

        if (Objects.isNull(type)){
            return Result.error("暂无数据");
        }

        List<Category> categoryType = categoryService.getCategoryType(type);

        log.info("**********通过type分类下拉查询，end");
        return CollUtil.isNotEmpty(categoryType)?Result.success("type分类下拉查询成功"):Result.error("type分类下拉查询失败");
    }

    @GetMapping("/page")
    public Result getCategoryPage(@RequestParam Integer page,@RequestParam Integer pageSize) {
        log.info("**********分页查询，begin");

        if (Objects.isNull(page ) && Objects.isNull(pageSize)){
            return Result.error("暂无数据");
        }

        PageBean<Category> categoryPageBean = categoryService.getCategoryPages(page,pageSize);

        log.info("**********分页查询，end");
        return Objects.nonNull(categoryPageBean)?Result.success(categoryPageBean):Result.error("分页查询失败");
    }
}
